package cn.kevinking.ai.mcp.provider.schema;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.github.victools.jsonschema.generator.OptionPreset;
import com.github.victools.jsonschema.generator.SchemaGenerator;
import com.github.victools.jsonschema.generator.SchemaGeneratorConfigBuilder;
import com.github.victools.jsonschema.generator.SchemaVersion;
import org.springframework.util.StringUtils;

/**
 * @author jinkw
 */
public class BfzSchemaGenerator {
    public static final SchemaVersion SCHEMA_VERSION = SchemaVersion.DRAFT_2019_09;
    public static final OptionPreset OPTION_PRESET = OptionPreset.PLAIN_JSON;
    public static final String PROPERTIES = "properties";
    public static final String TYPE = "type";
    public static final String OBJECT = "object";
    public static final String EMPTY = "";
    private final SchemaGenerator generator;

    public BfzSchemaGenerator() {
        SchemaGeneratorConfigBuilder configBuilder = new SchemaGeneratorConfigBuilder(SCHEMA_VERSION, OPTION_PRESET);
        this.generator = new SchemaGenerator(configBuilder.build());
    }

    public ObjectNode generate(Class<?> clazz) {
        return generator.generateSchema(clazz);
    }

    public boolean isObjectType(ObjectNode objectNode) {
        JsonNode typeNode = objectNode.get(TYPE);
        return typeNode != null && OBJECT.equals(typeNode.asText());
    }

    public String getType(ObjectNode objectNode) {
        return objectNode.get(TYPE).asText(EMPTY);
    }
}
